<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日志编码测试</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .container {
            background-color: #f5f5f5;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .success {
            color: green;
        }
        .error {
            color: red;
        }
    </style>
</head>
<body>
    <h1>日志编码测试</h1>
    
    <div class="container">
        <h2>测试说明</h2>
        <p>此页面用于测试系统中的中文日志编码是否正常。点击下方按钮发送包含中文的日志信息，然后查看后端日志文件是否正确显示中文内容。</p>
        
        <div>
            <button id="testSimpleLog">发送简单中文日志</button>
            <button id="testComplexLog">发送复杂中文日志</button>
            <button id="testLogUpload">测试日志上传</button>
        </div>
        
        <div id="result" class="result">
            测试结果将显示在这里...
        </div>
    </div>

    <script>
        // 获取结果显示区域
        const resultElement = document.getElementById('result');
        
        // 显示结果
        function showResult(message, isSuccess = true) {
            resultElement.innerHTML = `
                <p class="${isSuccess ? 'success' : 'error'}">${message}</p>
            `;
        }
        
        // 生成随机ID
        function generateRandomId() {
            return Math.random().toString(36).substring(2, 10);
        }
        
        // 发送日志请求
        async function sendLog(logData) {
            try {
                const timestamp = Date.now();
                const requestId = generateRandomId();
                
                // 构造完整的日志数据
                const fullLogData = {
                    requestId: requestId,
                    timestamp: timestamp,
                    logs: [logData],
                    environment: 'development',
                    clientIp: '127.0.0.1',
                    appVersion: '1.0.0'
                };
                
                // 获取token
                const token = localStorage.getItem('token');
                
                // 发送请求
                const response = await fetch('http://localhost:8080/api/logs/collect', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': token ? `Bearer ${token}` : ''
                    },
                    body: JSON.stringify(fullLogData)
                });
                
                if (response.ok) {
                    showResult(`日志发送成功！请求ID: ${requestId}<br>请检查后端日志文件 (logs/debug.log, logs/info.log, logs/error.log) 中的中文显示`);
                } else {
                    showResult(`日志发送失败！状态码: ${response.status}`, false);
                }
            } catch (error) {
                showResult(`日志发送异常: ${error.message}`, false);
            }
        }
        
        // 测试简单中文日志
        document.getElementById('testSimpleLog').addEventListener('click', () => {
            showResult('正在发送简单中文日志...');
            
            const logData = {
                level: 'INFO',
                message: '这是一条测试中文日志',
                timestamp: Date.now(),
                module: 'TestModule',
                user: '测试用户'
            };
            
            sendLog(logData);
        });
        
        // 测试复杂中文日志
        document.getElementById('testComplexLog').addEventListener('click', () => {
            showResult('正在发送复杂中文日志...');
            
            const logData = {
                level: 'DEBUG',
                message: '这是一条包含特殊字符的中文测试日志：测试平台功能验证',
                timestamp: Date.now(),
                module: 'LogEncodingTest',
                user: '系统管理员',
                details: {
                    功能测试: '日志编码',
                    测试结果: '验证中',
                    特殊字符: '！@#￥%……&*（）'
                }
            };
            
            sendLog(logData);
        });
        
        // 测试日志上传
        document.getElementById('testLogUpload').addEventListener('click', () => {
            if (window.Logger && window.Logger.uploadLogs) {
                showResult('正在调用Logger.uploadLogs()...');
                
                try {
                    window.Logger.uploadLogs().then(() => {
                        showResult('Logger.uploadLogs() 调用成功！请检查后端日志文件');
                    }).catch(error => {
                        showResult(`Logger.uploadLogs() 调用失败: ${error.message}`, false);
                    });
                } catch (error) {
                    showResult(`Logger.uploadLogs() 调用异常: ${error.message}`, false);
                }
            } else {
                showResult('Logger对象或uploadLogs方法不可用，请先加载Logger.js', false);
            }
        });
        
        // 页面加载时检查是否有token
        window.addEventListener('load', () => {
            const token = localStorage.getItem('token');
            if (!token) {
                resultElement.innerHTML += '<p class="error">注意：localStorage中未找到token，可能会导致401/403错误</p>';
            }
        });
    </script>
</body>
</html>